<template>
	<div class="texture-list table-theme">
		<head-title title="运营--材质列表"></head-title>
		<el-card>
			<div class="top-menu">
				<div class="menu-left">
					<select-view
						:option-list="statusList"
						:value="status"
						placeholder="排序"
						type="status"
						@value="setSelectValue"
					>
					</select-view>
				</div>
				<div class="menu-right">
					<refresh-button @refresh="refreshPage"></refresh-button>
					<search-view :option="searchOption" @value="goSearch" @clear="refreshPage"></search-view>
					<div class="add">
						<el-button type="primary" @click="add">添加材质</el-button>
					</div>
				</div>
			</div>
			<p class="data-number">总数据数：{{total}}</p>
			<el-table :data="textureList" border>
				<el-table-column label="材质ID" align="center" prop="texture_id"></el-table-column>
				<el-table-column label="材质名称" align="center" prop="texture_name"></el-table-column>
				<el-table-column label="排序" align="center" prop="sort" width="180"></el-table-column>
				<el-table-column label="上下架" align="center" width="180">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.texture_status" :active-value="1" :inactive-value="2" @change="upDown($event, scope.row.texture_id)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="添加时间" align="center" prop="add_time"></el-table-column>
				<el-table-column label="操作" align="center" width="150">
					<template slot-scope="scope">
						<el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
						<el-button type="danger" size="mini" @click="del(scope.row.texture_id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="pages">
				<el-pagination background layout="prev, pager, next, jumper" :total="total" @current-change="init" :current-page="page"></el-pagination>
			</div>
		</el-card>

		<!-- 添加材质弹框 -->
		<el-dialog :title="title" :visible.sync="addDialog" width="500px" center :modal-append-to-body="false">
			<el-form :model="addForm" :rules="addFormRules" ref="addForm" label-width="120px">
				<div class="class-info">
					<div class="class-exhibition">
						<el-form-item label="材质名称:" prop="texture_name">
							<el-input v-model="addForm.texture_name" placeholder="请输入材质名称" clearable></el-input>
						</el-form-item>
						<el-form-item label="排序:" prop="sort">
							<el-input type="number" onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" v-model="addForm.sort"
							 placeholder="请输入排序" clearable></el-input>
						</el-form-item>
						<el-form-item label="上下架:" prop="texture_status">
							<el-switch v-model="addForm.texture_status" :active-value="1" :inactive-value="2"></el-switch>
						</el-form-item>
					</div>
				</div>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialog = false">取 消</el-button>
				<el-button type="primary" @click="submit('addForm')">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
import SelectView from "@/components/SelectView";
	export default {
		components:{
			SelectView
		},
		data() {
			return {
				title: '添加材质',
				submitType: '', // 提交的类型 0 增加 1编辑
				imageUrl: '',
				addDialog: false,
				textureList: [],
				total: 0,
				totalData: 0,
				status: 1,
				statusList: [{
						value: 1,
						label: '上架'
					},
					{
						value: 2,
						label: '下架'
					}
				],
				page: 0,
				addForm: {
					texture_name: '',
					sort: '',
					texture_status: 1
				},
				addFormRules: {
					texture_name: [{
						required: true,
						message: '请输入材质名称'
					}],
					texture_status: [{
						required: true,
						message: '上下架'
					}],
					sort: [{
						required: true,
						message: '请输入排序'
					}]
				},
				searchOption:[{
					value:0,
					label:"名称"
				}],
				classSelect:0,
				selectContent:''
			};
		},
		watch: {
			'addForm.sort': function(val) {
				if (this.addForm.sort < 0) {
					this.addForm.sort = 0;
					this.$message.error('请输入正确的排序');
				}
			}
		},
		methods: {
			// 初始化列表
			init(e) {
				this.page = e;
				let data = {
					page: e - 1 ? e - 1 : 0,
					limit: 10,
					texture_status: this.status
				};
				if (this.selectContent != '') {
					data.keyword = this.selectContent;
				}
				this.$http(this.$ApiList.ClassTextureList, data).then(res => {
					this.textureList = res.datas.data;
					this.total = res.datas.count;
				});
			},

			// 上下架
			upDown(e, texture_id) {
				let data = {
					status: e,
					texture_id: texture_id
				};
				this.$http(this.$ApiList.TextureDown, data).then(res => {
					if (res.code == 200) {
						this.$layer.msg('操作成功');
						this.init();
					}
				});
			},
			// 搜素
			goSearch(classSelect, selectContent) {
				this.classSelect = classSelect;
				this.selectContent = selectContent;
				this.init();
			},
			// 增加材质
			add() {
				this.title = '添加材质';
				this.imageUrl = '';
				this.submitType = 0;
				this.addForm = {
					texture_name: '',
					sort: '',
					texture_status: 1,
					store_id: localStorage.getItem('store_id')
				};
				this.addDialog = true;
			},
			// 删除
			del(id) {
				this.$confirm('此操作将永久删除该材质, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						let data = {
							texture_id: id
						};
						this.$http(this.$ApiList.TextureDel, data).then(res => {
							if (res.code == 200) {
								this.$message({
									message: '删除成功',
									type: 'success'
								});
								this.init();
							}
						});
					})
					.catch(() => {});
			},
			// 编辑
			edit(content) {
				this.title = '编辑材质';
				this.submitType = 1;
				this.addForm = content;
				this.addDialog = true;
			},
			// 刷新
			refreshPage() {
				this.page = 1;
				this.status = 1;
				this.selectContent = '';
				this.init();
			},

			submit(formName) {
				this.$refs[formName].validate(valid => {
					if (valid) {
						// submitType 0新增 1编辑
						if (this.submitType == 0) {
							this.$http(this.$ApiList.UpTexture, this.addForm).then(res => {
								if (res.code == 200) {
									this.$layer.msg('提交成功');
									this.addDialog = false;
									this.init();
								}
							});
						} else {
							this.$http(this.$ApiList.UpdateTexture, this.addForm).then(res => {
								if (res.code == 200) {
									this.$layer.msg('编辑成功');
									this.addDialog = false;
									this.init();
								}
							});
						}
					} else {
						this.$layer.msg('请填写完整信息后提交');
					}
				});
			},
			setSelectValue(obj){
				const{ type,value } = obj
				switch (type) {
					case 'status':
						this.status = value
						break;
				
					default:
						break;
				}
				this.init()
			}
		},
		created() {
			this.init();
		}
	};
</script>
<style lang="scss" scoped>
	.texture-list{

	}
</style>
